package com.btr.uimeter.probes;

import java.util.LinkedList;
import java.util.List;

import com.btr.uimeter.UIMeterEvent;
import com.btr.uimeter.UIMeterEventHandler;

/*****************************************************************************
 * This class defines base class for all probes. All probes need to be registered
 * at the UIMeter.
 * <p>
 * The probes have a defined lifecycle. The are initially stopped.
 * When the UIMeter is started then the start() method of all registered probes is called
 * and the probe is considered running. 
 * When the UIMeter is shut down then it calls the stop method on all probes.   
 * </p>
 * A probe implementation must allow external component to register on it.
 * All registered listeners should then be notified about "probe events".  
 * 
 * @author Bernd Rosstauscher (java@rosstauscher.de) Copyright 2010
 ****************************************************************************/

public class UIMeterProbe {

	private List<UIMeterEventHandler> eventListenerList;

	/*************************************************************************
	 * Constructor
	 ************************************************************************/
	
	public UIMeterProbe() {
		super();
		eventListenerList = new LinkedList<UIMeterEventHandler>();
	}
	
	/*************************************************************************
	 * addEventHandler
	 ************************************************************************/
	
	public void addEventHandler(UIMeterEventHandler l) {
		this.eventListenerList.add(l);
	}
	
	/*************************************************************************
	 * removeEventHandler
	 ************************************************************************/
	
	public boolean removeEventHandler(UIMeterEventHandler l) {
		return this.eventListenerList.remove(l);
	}

	/*************************************************************************
	 * Fires an probe event and notifies all registered listeners.
	 * @param e the event to propagate.
	 ************************************************************************/
	
	protected void fireProbeEvent(UIMeterEvent e) {
		for (UIMeterEventHandler l : eventListenerList) {
			l.processEvent(this, e);
		}
	}
	
	/*************************************************************************
	 * Called to start the probing.
	 ************************************************************************/

	public void start() {
		// Override if needed
	}

	/*************************************************************************
	 * Called to stop the probing.
	 ************************************************************************/

	public void stop() {
		// Override if needed
	}
	
}
